/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.controller;

import br.com.jr2tecnologia.ConstantsOfSystem;
import br.com.jr2tecnologia.Fusioncharts;
import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.SMSMailUtil;
import br.com.jr2tecnologia.model.LoginPersistenceLocal;
import br.com.jr2tecnologia.model.SmsReceivedPersistenceLocal;
import br.com.jr2tecnologia.model.SmsSendPersistenceLocal;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.entity.SmsReceived;
import br.com.jr2tecnologia.model.entity.SmsSend;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ronildo
 */
@ManagedBean
@SessionScoped
public class LogonMBean implements Serializable {

    private static final Logger logger = LoggerFactory.getLogger();

    @EJB
    private LoginPersistenceLocal loginPersistence;
    @EJB
    private SmsSendPersistenceLocal smsSendPersistence;
    @EJB
    private SmsReceivedPersistenceLocal smsReceivedPersistence;

    private Login login;
    private String email;
    private String password;

    /** Creates a new instance of LogonMBean */
    public LogonMBean() {
    }

    public Login getLogin() {
        return login;
    }

    public void setLogin(Login login) {
        this.login = login;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String logout() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext .getExternalContext().getSession(false);
        session.invalidate();
        return ConstantsOfSystem.LOGON;
    }

    public String verifyLogin(){
        login = loginPersistence.verifyLogin(email);

        if(login != null){
            if(!login.getPassword().equals(password)){
                logger.info("Login "+login.getEmail()+" Failed. The password specified is not correct.");
                return null;
            }else if(login.getBlocked()){
                logger.info("Login "+login.getEmail()+" Failed. The login specified is blocked.");
                return null;
            }else{
                SMSMailUtil.putAttributeInSessionMap(ConstantsOfSystem.USER_SESSION_KEY, login);
                makeReportChart();
                return ConstantsOfSystem.DASHBOARD;
            }
        }else{
            logger.info("The login <"+ email + "> and email "+email+" not exists.");
            return null;
        }
    }

    private void makeReportChart(){
        List<SmsSend> listSmsSend = smsSendPersistence.list(new SmsSend(login));
        List<SmsReceived> listSmsReceived = smsReceivedPersistence.list(new SmsReceived(login));
        SimpleDateFormat sdf = new SimpleDateFormat("MM/yy");

        Map<String, Integer> smsSendOrdered = new HashMap<String, Integer>();
        List<String> keySmsSendOrdered = new ArrayList<String>();
        for(SmsSend smsSend : listSmsSend){
            String date = sdf.format(smsSend.getDatetimeRegistered());
            if(smsSendOrdered.containsKey(date)){
                 smsSendOrdered.put(date, smsSendOrdered.get(date) + 1);
            }else{
                keySmsSendOrdered.add(date);
                smsSendOrdered.put(date, 1);
            }
        }

        Map<String, Integer> smsReceivedOrdered = new HashMap<String, Integer>();
        List<String> keySmsReceivedOrdered = new ArrayList<String>();
        for(SmsReceived smsReceived : listSmsReceived){
            String date = sdf.format(smsReceived.getDatetimeReceived());
            if(smsReceivedOrdered.containsKey(date)){
                 smsReceivedOrdered.put(date, smsReceivedOrdered.get(date) + 1);
            }else{
                keySmsReceivedOrdered.add(date);
                smsReceivedOrdered.put(date, 1);
            }
        }

        Fusioncharts.makeXmlSmsSend(smsSendOrdered,keySmsSendOrdered,login.getPkLogin());
        Fusioncharts.makeXmlSmsReceived(smsReceivedOrdered,keySmsReceivedOrdered,login.getPkLogin());
    }
}